﻿/*
Main code for the icon dock
*/
// import Tweener Class
import caurina.transitions.Tweener;
import caurina.transitions.Equations;
// import analysisXML;
import XMLContentLoader;

//===================================================================================
// Initialization methods
//-----------------------------------------------------------------------------------

// xml
_root.a_xml = new XMLContentLoader("selection.xml", this);

this.init = function():Void {
	// Initializes the movieclip

	// Properties
	//this.icons = [this.nav0, this.nav1, this.nav2];
	this.iconNames = ["Dance", "Why you don't ", "Less and More"];

	this.zoomedAmmount = 0; // 0 when at rest, 1 when zoomed
	this.cursorPosition = undefined;
	this.isActivated = false;
	this.initContent();
	this.DropShadowFilter();

	// Finally, redraws
	this.redraw();
	this.stop();
	
	
};


//===================================================================================
// Execution methods
//-----------------------------------------------------------------------------------


this.redraw = function() {
	// Redraws every item on their new position
	var i:Number;

	var scaleSmall:Number = 1; // Default scale
	var scaleBig:Number = 1.5; // Scale when closer to the cursor
	var scaleFalloff:Number = 160; // Scale falloff distance, in screen pixels

	var baseX:Number = 300; // Center column for the dock
	var baseY:Number = backgroundBar._y ; // 180; // Base row for all icons

	var iconMargin:Number =  150; // Distance between the margins of the bounding box of each icon
	var assumedIconWidth:Number = 100; // Assumed icon width if scale is 1; this is used as a bounding box for the icons
	var assumedIconHeight:Number = 70; // Assumed icon height if scale is 1

	// Redraw: icons
	var defaultCols:Array = new Array(); // Default position of each icon when at rest - its mouseover position
	var itemScales:Array = new Array(); // Final scale of each icon
	var itemCols:Array = new Array(); // New horizontal position
	var totalWidth:Number = 0; // Total width of whole dock

	for (i = 0; i < this.icons.length; i++) {
		// Find the default position of each icon - this could be some separately one time only... but this way is a bit more dynamic
		defaultCols[i] = baseX + ((i-((this.icons.length-1)/2)) * ((assumedIconWidth * scaleSmall) + iconMargin));

		// Find the new scale of each icon depending on the mouse position
		var distanceFraction:Number = (this._xmouse - defaultCols[i]) / scaleFalloff; // -1 to 1 depending on the distance from the cursor
		if (distanceFraction < -1 || distanceFraction > 1) {
			// Too far, normal scale
			itemScales[i] = scaleSmall;
		} else {
			// Close enough to the cursor // easeInOutQuad is also good
			itemScales[i] = Equations.easeInOutSine(1-Math.abs(distanceFraction), scaleSmall, scaleBig - scaleSmall, 1);
		}

		// Set the new horizontal position of the icon
		itemCols[i] = baseX + totalWidth + (assumedIconWidth * itemScales[i] / 2);
	
		// Increase the position "head"
		totalWidth += assumedIconWidth * itemScales[i];
		if (i < this.icons.length - 1) totalWidth += iconMargin;

		// Set the vertical position
		itemRows[i] = baseY - (assumedIconHeight * itemScales[i] / 2);
	}

	// Calculates the total icon offset for correct centering
	var minMousePos:Number = defaultCols[0] - (scaleSmall * assumedIconWidth / 2);
	var maxMousePos:Number = defaultCols[defaultCols.length-1] + (scaleSmall * assumedIconWidth / 2);;
	var mouseFraction:Number = (this._xmouse - minMousePos) / (maxMousePos - minMousePos);
	mouseFraction = mouseFraction < 0 ? 0 : mouseFraction > 1 ? 1 : mouseFraction;
	var restOffset:Number = totalWidth - (maxMousePos-minMousePos);
	var iconOffset:Number = restOffset * (mouseFraction - 0.5);

	// Nudges the icons to their correct positions
	for (i = 0; i < this.icons.length; i++) {
		itemCols[i] -= iconOffset + (totalWidth/2);
	}

	var unZoomedAmmount:Number = 1-this.zoomedAmmount;

	// Finally, update the whole thing -- separated step because of the "zoomedAmmount" control
	for (i = 0; i < this.icons.length; i++) {

		var calculatedScale:Number = ((scaleSmall * unZoomedAmmount) + (itemScales[i] * this.zoomedAmmount));
		this.icons[i]._xscale = this.icons[i]._yscale = calculatedScale * 100;
		this.icons[i]._x = (itemCols[i] * this.zoomedAmmount) + (defaultCols[i] * unZoomedAmmount);
		this.icons[i]._y = baseY - (assumedIconHeight * calculatedScale / 2) + 15;
		this.icons[i].onRelease = on_release;
		this.icons[i].onRollOver = on_rollover;
		this.icons[i].onRollOut = on_rollout;
	}
	
	
	// Redraw: text
	// Crappy/cheap way to calculate. Should be a hit movieclip on the object itself.
	var closestItem:Number = undefined;
	var closestDistance:Number = undefined;
	for (i = 0; i < this.icons.length; i++) {
		var iDistance:Number = Math.abs(this.icons[i]._x - this._xmouse);
		if ((closestItem == undefined || iDistance < closestDistance) && (iDistance < assumedIconWidth * 0.5 * scaleBig)) {
			closestDistance = iDistance;
			closestItem = i;
		}
	}
	this.shorttext.text = closestItem == undefined || !isActivated ? "" : this.iconNames[closestItem];

	// Redraw: background bar
	var barMargin:Number = 8; // Horizontal bar margin
	//this.backgroundBar._y = baseY - this.backgroundBar._height;

	var widthZoomedIn:Number = totalWidth + (barMargin * 2);
	var widthZoomedOut:Number = (maxMousePos - minMousePos) + (barMargin * 2);
	//this.backgroundBar._width = (widthZoomedIn * this.zoomedAmmount) + (widthZoomedOut * unZoomedAmmount);

	var positionZoomedIn:Number = baseX - iconOffset - (totalWidth/2) - barMargin;
	var positionZoomedOut:Number = baseX - (widthZoomedOut / 2);

	///this.backgroundBar._x = (positionZoomedIn * this.zoomedAmmount) + (positionZoomedOut * unZoomedAmmount);
};

/**
 * name to id 
 * 
 * @usage  string
 * @return number 
 */

this.nameToID = function(_nameID:String){
	var name_array:Array = _nameID.split("nav");
	trace(name_array[1]);
	return Number(name_array[1]);
	}
// onRelease
this.on_rollover = function(){
	var _id = nameToID(this._name.toString());
	rollOverShow(_id);
	_root.shorttext._visible = true;
	_root.shorttext._x = this._x + 120;
	_root.shorttext._y = Stage.height-40;
}
this.on_rollout = function(){
	_root.shorttext._visible = false;
}
this.on_release = function(){
		trace(this._name);
		var ss:String = this._name.toString();
		var _id = nameToID(ss);		
		showBGBoardByID(_id);
		showAllTextByID(_id);
	}
this.onMouseMove = function() {
	if (this.isActivated) {
		// Already activated
		this.redraw();
		updateAfterEvent();
		// Check if it should be deactivated
		if (this._ymouse < 180) {
			deActivateDock();
		}
	} else {
		// Not activated yet
		// Check if it should be activated
		if (this._ymouse > 190) {
			activateDock();
		}
	}
};

this.activateDock = function() {
	this.isActivated = true;
	this.isDeactivating = false;
	Tweener.addTween(this, {zoomedAmmount:1, time:0.6, transition:"easeoutquint"});
	this.onEnterFrame = function() {
		this.redraw();
	};
};
this.deActivateDock = function() {
	this.isActivated = false;
	this.isDeactivating = true;
	Tweener.addTween(this, {zoomedAmmount:0, time:0.6, transition:"easeoutquint", onComplete:function() { delete this.onEnterFrame; }});
};

//===================================================================================
// loading methods
//-----------------------------------------------------------------------------------
//===================================================================================
function textEffect(_effect:String):Void{
	trace(_effect);
	switch (_effect) {
	case "fadein" :
		_root.context.highlight._alpha = 0;	
		_root.context.content._alpha = 0;
		_root.context.footer._alpha = 0;
		Tweener.addTween(_root.context.highlight, {_alpha:100, time:1});				
		Tweener.addTween(_root.context.content, {_alpha:100, time:1,delay:0.5});		
		Tweener.addTween(_root.context.footer, {_alpha:100, time:1,delay:1});		
		break;	  
	case "moveleft" :
		var _xdistance = 20;
		_root.context.highlight._alpha = 0;	
		_root.context.content._alpha = 0;
		_root.context.footer._alpha = 0;
		_root.context.highlight._x += _xdistance;	
		_root.context.content._x   += _xdistance;
		_root.context.footer._x    += _xdistance;
		Tweener.addTween(_root.context.highlight, {_x:_x-_xdistance,_alpha:100, time:0.8});				
		Tweener.addTween(_root.context.content, {_x:_x-_xdistance,_alpha:100, time:0.8,delay:0.5});		
		Tweener.addTween(_root.context.footer, {_x:_x-_xdistance,_alpha:100, time:0.8,delay:1});		
		break;
	case "moveright" :
		var _xdistance = 20;
		_root.context.highlight._alpha = 0;	
		_root.context.content._alpha = 0;
		_root.context.footer._alpha = 0;
		_root.context.highlight._x -= _xdistance;	
		_root.context.content._x   -= _xdistance;
		_root.context.footer._x    -= _xdistance;
		Tweener.addTween(_root.context.highlight, {_x:_x+_xdistance,_alpha:100, time:0.8});				
		Tweener.addTween(_root.context.content, {_x:_x+_xdistance,_alpha:100, time:0.8,delay:0.5});		
		Tweener.addTween(_root.context.footer, {_x:_x+_xdistance,_alpha:100, time:0.8,delay:1});		
		break;
		/*
	case "movedown" :
		var _ydistance = 20;
		_root.context.highlight._alpha = 0;	
		_root.context.content._alpha = 0;
		_root.context.footer._alpha = 0;
		_root.context.highlight._y += _ydistance;	
		_root.context.content._y   += _ydistance;
		_root.context.footer._y    += _ydistance;
		
		Tweener.addTween(_root.context.highlight, {_y:0,_alpha:100, time:0.8});				
		Tweener.addTween(_root.context.content, {_y: 30,_alpha:100, time:0.8,delay:0.5});		
		Tweener.addTween(_root.context.footer, {_y: 90,_alpha:100, time:0.8,delay:1});		
		break;
	case "moveup" :
		var _ydistance = 20;
		_root.context.highlight._alpha = 0;	
		_root.context.content._alpha = 0;
		_root.context.footer._alpha = 0;
		_root.context.highlight._y -= 20;	
		_root.context.content._y   -= 20;
		_root.context.footer._y    -= 20;		
		Tweenwe.addTween(_root.context.highlight,{_y:_y+20, _alpha:100,time:0.8});
		Tweenwe.addTween(_root.context.content,{_y:_y+20, _alpha:100,time:0.8,delay:0.5});
		Tweenwe.addTween(_root.context.footer,{_y:_y+20, _alpha:100,time:0.8,delay:1});
		break;
		*/
	default :
	  trace("no effect");
	  break;
	}	
}

// show AllText By ID
this.showAllTextByID = function(_id:Number){
	textEffect(_root.a_xml.effectArray[_id]);
	_root.context.highlight.label.htmlText = _root.a_xml.highlightArray[_id];
	_root.context.highlight.label.filters = _root.a_xml.filterArray;	
	_root.context.content.label.htmlText = _root.a_xml.contentArray[_id];
	_root.context.content.label.filters = _root.a_xml.filterArray;
	_root.context.footer.label.htmlText = _root.a_xml.footerArray[_id];	
	_root.context.footer.label.filters = _root.a_xml.filterArray;
	var pos_array:Array = _root.a_xml.positionArray[_id].toString().split(",");
	_root.context._x = Number(pos_array[0]);
	_root.context._y = Number(pos_array[1]);
	_root.context.onRelease = function(){
		getURL(_root.a_xml.linkArray[_id],"_blank");
	}
}
//different thumbnail onrollOver shows different shorttext
this.rollOverShow = function(_id:Number){		
	_root.shorttext.label.htmlText = _root.a_xml.shorttextArray[_id];
	_root.shorttext.label.filters = _root.a_xml.filterArray;	
}
//show BGBoard By ID
this.showBGBoardByID = function(_id:Number){
	_root.a_xml.load_BGBoard(_id);
}

/**
 * init the content text and BGBoard on the Stage 
 * by a random number
 * @usage   
 * @return  
 */

this.initContent = function(){	
	trace('_root.a_xml.idArray.length: ' + _root.a_xml.idArray.length)
	var myNumber:Number = random(_root.a_xml.idArray.length);
	this.showBGBoardByID(myNumber);
	this.showAllTextByID(myNumber);
}

//DropShadowFilter return array


/*
this.initContext = function(){	
	_root.context.highlight.label.htmlText = _root.a_xml.highlightArray[0];
	_root.context.content.label.htmlText = _root.a_xml.contentArray[0];
	_root.context.footer.label.htmlText = _root.a_xml.footerArray[0];	
	_root.shorttext.label.htmlText = _root.a_xml.shorttextArray[0];
	var pos_array:Array = _root.a_xml.positionArray[0].toString().split(",");
	_root.context._x = Number(pos_array[0]);
	_root.context._y = Number(pos_array[1]);
}
*/